var meterManageData = [];
var chargingStationData = [];
var flagDate = undefined;
var deleteFlag = undefined;
var deleteIndex = undefined;
var deleteCount = undefined;
var parentNameData = [];

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	$ajaxList('chargingStation', getChargingStation, {BulidStatus: '3'});
	createTableHead("meterManageThead", meterManageTable);
	search();
	setTimeout(hideLoading, 500);
});
function getChargingStation(data) {
	var length = data.length;
	chargingStationData = data;
	var item = undefined;
	for (var i = 0; i < length; i++) {
		$("#ChargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
		item = {};
		item.ID = data[i].chargingStationID;
		item.label = data[i].chargingStationName;
		item.value = data[i].chargingStationName;
		parentNameData.push(item);
	}
	$(function(){
		autoComplete("name", parentNameData, '', "ChargingStationID");
		bindInput("name", onInput);  
	});
}
function onInput(){
	$("#ChargingStationID").val('');
}
function getChargingStationID(){
	var value = $("#name").val();
	if (!value) {
		$("#ChargingStationID").val('');
	} else {
		if (!$("#ChargingStationID").val() && parentNameData && parentNameData.length > 0) {
			var matcherData = filter(parentNameData, value);
			if (matcherData) {
				var length = matcherData.length;
				for (var i=0; i<length; i++) {
					if (matcherData[i] && matcherData[i].value == value) {
						$("#ChargingStationID").val(matcherData[i].ID);
						break;
					}
				}
			}
		}
	}
}

function search() {
	$ajax('searchMeterManage', 'meterManageSearch', searchMeterManege);
}
function searchMeterManege(data){
	meterManageData = data && data.meterManageList || [];
	createTableBody("meterManageTbody", meterManageTable, meterManageData, true, true);
	setTimeout(hideLoading, 500);
}
function chkBefore(index, chkFlag){
	if (chkFlag == "update") {
		update(index);
	} else if(chkFlag == "delete") {
		deleteIndex = index;
		deleteFlag = "delete"
		showMessge("删除电表信息可能会影响结算流程，确定删除吗？", "model2");
	}
}
function save() {
	deleteFlag = "save";
	addTitle("meterManageSave");
	allTable();
}
function update(index) {
	$('#title').html('');
	deleteFlag = "update";
	addTitle("meterManageUpdate");
	allTable(index);
}
function allTable(index){
	createComBody("detailTwoBody", meterManageTable, meterManageData[index], 1);
	for (var i = 0;i < chargingStationData.length;i++){
        $("#chargingStationID").append("<option value='" + chargingStationData[i].chargingStationID + "'>" + chargingStationData[i].chargingStationName + "</option>");  
	}
	if(deleteFlag == "update") {
		$("#chargingStationID").val(meterManageData[index].chargingStationID);
		$("#id").val(meterManageData[index].id);
		updateIndex = index;
		if(meterManageData[index].enabled == true){
			$("#enabled").val(1);
		}else{
			$("#enabled").val(0);
		}
	}
	$("#save").modal("show");
}
function confirm(){
	return checkForm("saveForm", meterManageTable);
}
function getUrl(){
	if(deleteFlag == "save"){
		return 'saveMeterManage';
	} else if(deleteFlag == "update"){
		return 'updateMeterManage';
	} else if(deleteFlag == "delete"){
		return 'deleteMeterManage';
	}
}
function getData(){
	return {ID: meterManageData[deleteIndex].id};
}
function saveMeterManage(data){
	if(deleteFlag == "save"){
	     var result = data && data.result;
	     if (result == "Success") {
	    	 showMessge(messageContent["Insert.Success"]);
	    	 $('#save').modal('hide');
	    	 formReset("saveForm");
	     } else if (result == "NotFound"){
	    	 showMessge(messageContent["Insert.Failed"]);
	     } else {
	    	 showMessge(messageContent["Insert.Error"]);
	     }
	     search();
	}else if(deleteFlag == "update"){
	     var result = data && data.result;
	     if (result == "Success"){
	    	 showMessge(messageContent["Update.Success"]);
	    	 $('#save').modal('hide');
	    	 formReset("saveForm");
	     } else if (result == "NotFound"){
	    	 showMessge(messageContent["Update.Failed"]);
	     } else{
	    	 showMessge(messageContent["Update.Error"]);
	     }
	     search();
	}
}
function sure(data){
    var result = data && data.result;
    if (result == "Success") {
		showMessge(messageContent["Delete.Success"]);
	} else if (result == "NotFound") {
		showMessge(messageContent["Delete.NotFound"]);
	} else {
		showMessge(messageContent["Delete.Error"]);
	}
    search();
}